package foundation.图论;

import java.util.Arrays;

/**
 * @author hang an
 * @since 2025/7/14
 */
public class 链式前向星 {
    static int N = 10010;
    // 头指针数组
    static int[] h = new int[N];
    static int[] to = new int[N];
    static int[] next = new int[N];
    static int[] weight = new int[N];
    static int idx;
    static int n = 0;

    private static void init() {
        idx = 0;
        Arrays.fill(h, 0);
    }

    // u -> v 权重为 w
    private static void add(int u, int v, int w) {
        next[idx] = h[u];
        to[idx] = v;
        weight[idx] = w;
        h[u] = idx++;
    }

    private static void display() {
        for (int i = 1; i <= n; i++) {
            System.out.print(i + "(邻居) : ");
            for (int ei = h[i]; ei > 0; ei = next[ei]) {
                System.out.print(to[ei] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        init();
        n = 5;
        int[][] arr = {
                {1, 2, 3},
                {1, 3, 3},
                {2, 4, 3},
                {2, 5, 3},
                {3, 5, 3},
                {4, 5, 3},
        };
        for (int[] edge : arr) {
            add(edge[0], edge[1], edge[2]);
        }
        display();
    }


}
